﻿Public Class Form6GongWei
    Public usr As String
    Public status As String
    Dim objGt As New clsGt
    Dim objGtProcedure As New clsGtProcedure
    Dim objGtProcedureDetails As New clsGtProcedureDetails
    Dim dt As DataTable
    Dim dgvSelectedId As String
    Private Sub Form6GongWei_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        setData()
    End Sub

    Public Sub setData()
        dt = objGt.getCurrentGt("0", status, "1")
        dt.Columns.Add("oper_type_name")
        If dt.Rows.Count > 0 Then
            For Each row As DataRow In dt.Rows
                If row("oper_type") = "0" Then
                    row("oper_type_name") = "骨架板"
                Else
                    row("oper_type_name") = "法兰板"
                End If
            Next
        End If
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dt
        For index = 0 To dgv.Rows.Count - 1
            dgv.Rows(index).Selected = False
        Next
        For index = 0 To dgv.Rows.Count - 1
            If dgv.Rows(index).Cells("sys_id").Value.ToString() = dgvSelectedId Then
                dgv.Rows(index).Selected = True
                Exit For
            End If
        Next
    End Sub

    Public Sub setDgvData()
        Dim dtProcedure As DataTable = objGtProcedure.selectGtProcedure(dgv.SelectedRows(0).Cells("sys_id").Value.ToString(), "2")
        DataGridView3.AutoGenerateColumns = False
        DataGridView3.DataSource = dtProcedure
        Dim dtOper As DataTable = objGtProcedureDetails.getGtProcedureDetailsList(dgv.SelectedRows(0).Cells("sys_id").Value.ToString(), "2")
        dgvOper.AutoGenerateColumns = False
        dgvOper.DataSource = dtOper
    End Sub

    Private Sub dgv_MouseClick(sender As Object, e As MouseEventArgs) Handles dgv.MouseClick
        If dgv.Rows.Count > 0 Then
            dgvSelectedId = dgv.Rows(dgv.CurrentRow.Index).Cells("sys_id").Value.ToString()
            setDgvData()
        End If
    End Sub

    Private Sub dgv_SelectionChanged(sender As Object, e As EventArgs) Handles dgv.SelectionChanged
        'If dgv.SelectedRows.Count > 0 Then
        '    dgvSelectedId = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
        '    setDgvData()
        'End If
    End Sub

    Private Sub btnOper_Click(sender As Object, e As EventArgs) Handles btnOper.Click
        If dgv.SelectedRows.Count > 0 Then
            Dim formOper As New FormOper()
            formOper.usr = usr
            formOper.source = "6gw"
            formOper.b_gt_sys_id = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
            formOper.ok_num_all = dgv.SelectedRows(0).Cells("ok_num").Value.ToString()
            If DataGridView3.SelectedRows.Count > 0 Then
                formOper.oper_num_all = DataGridView3.SelectedRows(0).Cells("Col_oper_num").Value.ToString()
            Else
                formOper.oper_num_all = "0"
            End If
            formOper.current_procedure_No = 2
            formOper.ShowDialog(Me)
        End If
    End Sub

    Private Sub btnValidityOper_Click(sender As Object, e As EventArgs) Handles btnValidityOper.Click
        If dgvOper.SelectedRows.Count > 0 Then
            If dgvOper.SelectedRows(0).Cells("Colvalidity").Value.ToString().Equals("1") Then
                If MsgBox("确认要进行作废吗？", vbYesNo, "提示") = vbYes Then
                    Dim sys_id As String = dgvOper.SelectedRows(0).Cells("Colsys_id").Value.ToString()
                    Dim procedure_no As String = dgvOper.SelectedRows(0).Cells("Colprocedure_no").Value.ToString()
                    Dim b_gt_sys_id As String = dgvOper.SelectedRows(0).Cells("Colb_gt_sys_id").Value.ToString()
                    Dim oper_num As String = dgvOper.SelectedRows(0).Cells("Coloper_num").Value.ToString()
                    Dim rtn As Integer = objGtProcedureDetails.updateValidity(sys_id, oper_num, b_gt_sys_id, procedure_no, usr)
                    If rtn <> -2 Then
                        MsgBox("作废成功")
                        setDgvData()
                    Else
                        MsgBox("作废失败")
                    End If
                End If
            Else
                MsgBox("该记录已作废")
            End If
        End If
    End Sub

    Private Sub btnFactoryOutsource_Click(sender As Object, e As EventArgs) Handles btnFactoryOutsource.Click
        If dgv.SelectedRows.Count > 0 Then
            Dim sys_id As String = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
            Dim assist_marker_jnj As String = dgv.SelectedRows(0).Cells("Col_assist_marker_jnj").Value.ToString()
            Dim formFactoryOutsourceSave As New FormFactoryOutsourceSave()
            formFactoryOutsourceSave.source = "6gw"
            formFactoryOutsourceSave.sys_id = sys_id
            formFactoryOutsourceSave.assist_marker_jnj = assist_marker_jnj
            formFactoryOutsourceSave.ShowDialog(Me)
        End If
    End Sub

    Private Sub btnValidity_Click(sender As Object, e As EventArgs) Handles btnValidity.Click
        If dgv.SelectedRows.Count > 0 Then
            Dim rtn_validity As Integer = objGt.update_validity(dgv.SelectedRows(0).Cells("sys_id").Value.ToString())
            If rtn_validity <> -2 Then
                MsgBox("作废成功"， vbYes, "提示")
                setData()
            Else
                MsgBox("作废失败", vbYes, "提示")
            End If
        End If
    End Sub
End Class